package cn.itcast.tanhua.mytanhuaserver.service.impl;

import cn.itcast.tanhua.mytanhuaserver.mapper.UserMapper;
import cn.itcast.tanhua.mytanhuaserver.service.UserService;
import cn.itcast.tanhua.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 校验token，获取user信息
 * @author 郭文亮
 * @since 2021/11/25 21:24
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${tanhua.sso.url}")
    private String urlSso;

    @Autowired
    private UserMapper userMapper;

    /**
     * 校验token，获取user信息，主要是user的id
     * @param token 包含user的id
     * @return 获取的user信息
     */
    @Override
    public User queryUserByToken(String token) {
        String url = urlSso + "/user/" + token;
        User u = this.restTemplate.getForObject(url, User.class);
        System.out.println(u);
        return u;
    }

    /**
     * 通过id查询用户数据
     *
     * @param userId
     * @return
     */
    @Override
    public User queryUserByUserId(Long userId) {
        return userMapper.selectById(userId);
    }
}
